#include "TriangleObstacleFactory.h"

TriangleObstacleFactory::TriangleObstacleFactory(void)
{
}

TriangleObstacleFactory::~TriangleObstacleFactory(void)
{
}

Element * TriangleObstacleFactory::createElement( XMLElement * element) {
	return this->createTriangleObstacle(element);
}

TriangularObstacle * TriangleObstacleFactory::createTriangleObstacle(XMLElement * element) {
	TriangularObstacle * obstacle = NULL;
	Position * arrVertices[3];

	if(element->getName()->compare("triangulo") == 0) {
		list<XMLElement*> vertices = element->getElementsByName("vertice");
		list<XMLElement*>::iterator iterVertices;

		int i=0;
		for (iterVertices = vertices.begin();iterVertices != vertices.end(); iterVertices++) {
			XMLElement * verticeElement = *iterVertices;
			Position * vertice = FactoryUtil::getInstance()->buildPosition(verticeElement);
			arrVertices[i++] = vertice;
		}
		obstacle = new TriangularObstacle(arrVertices[0],arrVertices[1],arrVertices[2]);
	}
	try {
		obstacle->setLineColor(FactoryUtil::getInstance()->buildColor(element->getAttributeByName("colorLinea")->getValue()->c_str()));
	}catch(NoSuchXmlElementException e) {
		obstacle->setLineColor(NULL);
	}
	try {
		obstacle->setElementColor(FactoryUtil::getInstance()->buildColor(element->getAttributeByName("colorElemento")->getValue()->c_str()));
	}catch(NoSuchXmlElementException e) {
		obstacle->setElementColor(NULL);
	}		
	obstacle->setTexture(FactoryUtil::getInstance()->requestTextureForElement(element));
	obstacle->setPosition(obstacle->getP1());

	return obstacle;
}